package 力扣日常刷题.木23一月.第15天315;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 帅小伙
 * @date 2022/3/15
 * @description
 */
public class Demo72死锁{


    public static void main(String[] args) {

        Demo72死锁 test = new Demo72死锁();
        for (int i = 0; i < 2; i++) {
            int t = i;
            new Thread(()->{test.run(t%2==0);},i+"").start();
        }

    }

    public void run (boolean flag) {
        if(flag){
            synchronized(A.class){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("A执行了");
                synchronized(B.class){
                    System.out.println("A执行结束");
                }
            }
        }else{
            synchronized(B.class){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B执行了");
                synchronized(A.class){
                    System.out.println("B执行结束");
                }
            }
        }

    }




    class A{

    }

    class B{

    }

}



